home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Audio-DSP / NU / Source / WorkspaceManager.m < prev    next >
Encoding:
Text File  |  1993-02-22  |  7.8 KB  |  320 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "WorkspaceManager.h"
  5. #import "MenuManager.h"
  6. #import "ClassManager.h"
  7. #import "NuString.h"
  8. #import "NuText.h"
  9. #import "MenuManager.h"
  10. #import "PrefsManager.h"
  11. #import <appkit/NXRTFDErrors.h>
  12. #import <appkit/Application.h>
  13. #import <appkit/Text.h>
  14. #import <appkit/Panel.h>
  15. #import <appkit/NXBrowser.h>
  16. #import <appkit/ScrollView.h>
  17. #import <appkit/TextField.h>
  18. #import <appkit/OpenPanel.h>
  19. #import <defaults/defaults.h>
  20. #import <objc/List.h>
  21. #import <objc/objc-load.h>
  22. #import <objc/Storage.h>
  23. #import <stdlib.h>
  24. #import <strings.h>
  25. #import  <sys/file.h>
  26. #import <sys/types.h>
  27. #import <sys/stat.h>
  28. #import <libc.h>
  29. #include <ctype.h>
  30.  
  31. extern id Nu ;
  32.  
  33. @implementation WorkspaceManager
  34.  
  35. - addToLineList: (int) lineNum ;
  36. { // add lineNum to the list of lines
  37.   [lineList addElement: &lineNum] ;
  38.   return self ;
  39. }
  40.  
  41.  
  42. - (char *) bareFileName ;
  43. { // return pointer to my fileName, minus any path
  44.   char *cursor ;
  45.   if(cursor = rindex(fileName, '/'))
  46.     cursor++ ;
  47.   else
  48.     cursor = fileName ;
  49.   return cursor ;
  50. }
  51.  
  52. - evalSelection:sender
  53. { return [textView evalSelection: sender] ;
  54. }
  55.  
  56.  
  57. - extension: (char *) extension ;
  58. { // check that fileName has the given extension...
  59.   // if not, then make sure it does.  extension should
  60.   // not include the "." character
  61.   char * currentExt ;
  62.   currentExt =  rindex(fileName,'.') ;
  63.   if(!currentExt) 
  64.   { // currently has no extension at all...
  65.     strcat(fileName,".") ;
  66.     strcat(fileName,extension) ;
  67.   }
  68.   // if current extension if rtf and it should be rtfd, then
  69.   // just plunk on a d.  ...
  70.   else if(!strcmp(currentExt,".rtf") && !strcmp(extension,"rtfd"))
  71.   {  // must remove the .rtf file
  72.      struct stat statBuf ;
  73.      if(!stat(fileName,&statBuf))
  74.        unlink(fileName) ;
  75.      strcat(fileName,"d") ;
  76.   }
  77.   // now reflect new name in our window...
  78.   [self setFancyTitle] ;
  79.   return self ;
  80. }
  81.  
  82. - (char *) extension ;
  83. { // provide file extension
  84.   if([textView isRTFD])
  85.    return "rtfd" ;
  86.   else
  87.     return "rtf" ;
  88. }
  89.  
  90. - (char *) fileName ;
  91. { return fileName ;
  92. }
  93.  
  94. - fileName: (char *) fname ;
  95. { // copy fname into ivar, set the
  96.   // window's title to fname, with the
  97.   // filename followed by the path
  98.   if(fileName)
  99.   { if(!strcmp(fileName,fname))
  100.       return self ; // fileName isn't changing
  101.     else
  102.       free(fileName) ;
  103.   }
  104.   // allocate memory for fileName; leave room to
  105.   // add an extension
  106.   fileName = (char *) malloc(strlen(fname) + 8) ;
  107.   strcpy(fileName, fname) ;
  108.   return [self setFancyTitle] ; // set title of window
  109.   return self ;
  110. }
  111.  
  112. - free ;
  113. { free(fileName) ;
  114.   return [super free] ;
  115. }
  116.  
  117. - init ;
  118. { [textView setDelegate: self] ;
  119.   [self setDelegate: Nu] ;
  120.   lineList = [[Storage alloc] initCount: 0
  121.                        elementSize: sizeof(int)
  122.                        description: "i"] ;
  123.   // make sure we are not monofont so we can
  124.   // do rich text
  125.   [textView setMonoFont: NO] ;
  126.   isRTF = YES ;
  127.   [self setMiniwindowIcon: "wsdIcon.tiff"] ;
  128.   return self ;
  129. }
  130.     
  131. - lineList ;
  132. { // return the Storage List of lineNums
  133.   return lineList ;
  134. }
  135.  
  136. - message: (char *) aMsg ;
  137. { // show aMsg in the msgField
  138.   [msgField setStringValue: aMsg] ;
  139.   NXPing() ;
  140.   [self flushWindow] ;
  141.   return self ;
  142. }
  143.  
  144.  
  145. - readFile ;
  146. // read "fileName" into the text object. 
  147. { if(fileName[0] != '\0')
  148.   { [self message: "Reading file..."] ;
  149.     if(!strcmp(&fileName[strlen(fileName) - 4],".rtf"))
  150.     { // open an rtf file
  151.       NXStream *fileStream ;
  152.       if((fileStream = NXMapFile(fileName,NX_READONLY)) != NULL)
  153.         [textView readRichText: fileStream] ;
  154.       NXCloseMemory(fileStream,NX_FREEBUFFER);
  155.     }
  156.     else if(!strcmp(&fileName[strlen(fileName) - 5],".rtfd"))
  157.     { // open an rtfd file package
  158.       if([textView openRTFDFrom: fileName] != NX_RTFDErrorNone) 
  159.          NXRunAlertPanel("Nu", "Error, couldn't read: %s",
  160.           NULL,NULL,NULL,fileName) ;
  161.     }
  162.     else 
  163.     { // open file which is neither rtf nor rtfd.
  164.       NXStream *fileStream ;
  165.       if((fileStream = NXMapFile(fileName,NX_READONLY)) != NULL)
  166.         [textView readText: fileStream] ;
  167.       NXCloseMemory(fileStream,NX_FREEBUFFER);
  168.     }
  169.   }
  170.   [self message: ""] ;
  171.   [self setDocEdited: NO] ;
  172.   return self ;
  173. }
  174.  
  175. - removeFromLineList: (int) lineNum ;
  176. { // remove lineNum from the list of lines
  177.   if(lineList != nil)    
  178.    [lineList removeAt: lineNum] ;
  179.   return self ;
  180. }
  181.   
  182. -revert:sender ;
  183. { if([self isDocEdited])
  184.   { int rval = NXRunAlertPanel(
  185.      "Nutation","This window has been edited. Revert?",
  186.       "REVERT","CANCEL",NULL) ;
  187.     if(rval)
  188.       return [self readFile] ;
  189.     else
  190.       return nil ;
  191.   }
  192.   return self ;
  193. }
  194.  
  195. -(BOOL) save:sender ;
  196. { if([self isDocEdited])
  197.   { if(!fileName) // no fileName, i.e. first save:
  198.       return [self saveAs: sender] ;
  199.     else
  200.       return [self saveTextToFileName] ;
  201.   }
  202.   return YES ;
  203. }
  204.  
  205.  
  206. -(BOOL) saveAs:sender ;
  207. { id savePanel = [SavePanel new] ;
  208.   [savePanel setRequiredFileType: [self extension]] ;
  209.   if([savePanel runModalForDirectory: NULL file: fileName])
  210.   { [self fileName: (char *) [savePanel filename]] ;
  211.     return [self saveTextToFileName] ;
  212.   }
  213.   else
  214.     return NO ;
  215. }
  216.  
  217.  
  218. -(BOOL) saveTextToFileName ;
  219. // pre:  -ivar fileName contains a valid
  220. //       file pathname.
  221. //       -ivar textView contains a TextView object
  222. // post: if file can be opened or created, with mode
  223. //       644: text of textView is written out to the
  224. //       file named by fileName; file is closed, and
  225. //       YES is returned.
  226. //       otherwise returns NO
  227. { // removeBackup should be a dwrite...some day!
  228.   int rval ;
  229.   [self message: "Saving file..."] ;
  230.   if([textView isRTFD])
  231.   { const char *theFlags ;
  232.     [self extension: "rtfd"] ; // ensure rtfd extension
  233.     theFlags = NXGetDefaultValue([NXApp appName],"Flags") ;
  234.     rval = [textView saveRTFDTo:fileName 
  235.                  removeBackup: theFlags[BACKUPRTF]  == '1' 
  236.                  errorHandler: NULL] ;
  237.     if(rval != NX_RTFDErrorNone)
  238.     { NXRunAlertPanel("Nu","Couldn't save: %s\n",
  239.       NULL,NULL,NULL,fileName) ;
  240.       return NO ;
  241.     }
  242.   }
  243.   else // we should check for rtf errors...some day!
  244.   { NXStream *aStream = NXOpenMemory(NULL,0, NX_WRITEONLY);
  245.     [self extension: "rtf"] ;       
  246.     [textView writeRichText:aStream];    
  247.     NXSaveToFile(aStream, fileName);
  248.     NXCloseMemory(aStream, NX_FREEBUFFER);    
  249.   }
  250.   [self message: ""] ;
  251.   [self setDocEdited: NO] ;
  252.   return YES ;
  253. }
  254.  
  255. - setFancyTitle ;
  256. { // update the title from the fileName
  257.   char *fullName, *name ;
  258.   int nameLen ;
  259.   nameLen = strlen(fileName) + 1 ;
  260.   fullName = (char *) alloca(nameLen) ;
  261.   strcpy(fullName, fileName) ;
  262.   name = rindex(fullName,'/') ;
  263.   if(name) // is there is a path,
  264.   { char title[nameLen] ;
  265.     name[0] = '\0' ; // split string into 2 pieces
  266.     name++ ;
  267.     sprintf(title,"%s  %s",name,fullName) ;
  268.     [self setTitle: title] ;
  269.   }
  270.   else // no path, just use the filename
  271.     [self setTitle: fileName] ;
  272. }
  273.  
  274. - setTextView: sender ;
  275. { // remove sender's docView from hierarchy and replace
  276.   // with an instance of MMText 
  277.   textView = [sender docView] ;
  278.   return self ;
  279. }
  280.  
  281. - textDidGetKeys:text isEmpty:(BOOL)empty ;
  282. { // this is a delegate message for the textView
  283.   if(![self isDocEdited])
  284.      [self setDocEdited: YES] ;
  285.   return NO;
  286. }
  287.  
  288. - textWillConvert:sender fromFont:from toFont:to ;
  289. { if(![self isDocEdited])
  290.    [self setDocEdited: YES] ;
  291.   return to ;
  292. }
  293.  
  294. - textView ;
  295. { return textView ;
  296. }
  297.  
  298. - read: (NXStream *) aStream ;
  299. { [super read: aStream] ;
  300.   NXReadArray(aStream, "c", 256, fileName) ;
  301.   textView = NXReadObject(aStream) ;
  302.   msgField = NXReadObject(aStream) ;
  303.   lineList = NXReadObject(aStream) ;
  304.   [self setDelegate: NXReadObject(aStream)] ;
  305.   return self ;
  306. }
  307.  
  308. - write: (NXStream *) aStream ;
  309. { [super write: aStream] ;
  310.   NXWriteArray(aStream, "c", 256, fileName)  ;
  311.   NXWriteObjectReference(aStream,textView) ;
  312.   NXWriteObjectReference(aStream,msgField) ;
  313.   NXWriteObject(aStream,lineList) ;
  314.   NXWriteObjectReference(aStream,delegate) ;
  315.   return self ;
  316. }
  317.  
  318.  
  319. @end
  320.